#!/usr/bin/env python
#
# Copyright 2008 Doug Hellmann.
#
"""Load manually entered data from VTR (vague text record) files.

See http://code.google.com/p/athens-docket/wiki/VagueTextRecord
for details.
"""

__version__ = "$Id: load_data.py 126 2008-06-22 12:46:51Z doug.hellmann $"

# Import system modules
from django.core.management import setup_environ

import logging
import optparse
import os
import sys

sys.path.append(os.getcwd()) # Try to ensure we can find settings.py

# Import local modules

# Module

def main():
    """Main program to load data into the db.
    """
    
    option_parser = optparse.OptionParser(usage='usage: %prog [options] filename')
    option_parser.add_option('-d', '--debug',
                             dest='debug',
                             default=False,
                             action='store_true', 
                             help='Enable debugging messages')
    option_parser.add_option('-n', '--no-store', 
                             dest='store', 
                             default=True, 
                             action='store_false', 
                             help='Test but do not store the data')    
    option_parser.add_option('-v', '--verbose', 
                             dest='verbose', 
                             default=False, 
                             action='store_true', 
                             help='Produce verbose output')    
    (options, args) = option_parser.parse_args()
    
    if not args:
        raise RuntimeError('Please specify at least one filename to load.')

    if options.debug:
        level = logging.DEBUG
    else:
        level = logging.INFO
    logging.basicConfig(level=level,
                        format='%(levelname)-8s %(message)s',
                        )

    # Establish our django set-up so we can import models
    # to work with the database.
    import settings
    setup_environ(settings)

    # Now we can import the other modules we need that depend on django
    from vtr_loader import load_data_from_stream, ErrorKeeper
    
    errors = ErrorKeeper()
    num_new = 0
    num_existing = 0
    for filename in args:
        logging.info('Processing %s', filename)
        f = open(filename, 'rt')
        try:
            counts = load_data_from_stream(
                f, 
                store=options.store, 
                verbose=options.verbose, 
                message_stream=sys.stdout, 
                errorcb=errors,
                )
        finally:
            f.close()
        
        num_existing += counts[0]
        num_new += counts[1]

        for error in errors.errors:
            error['file'] = filename

    if options.verbose:
        print
        print 'Found %d new records' % num_new
        print 'Found %d existing records' % num_existing
        print 'Found %d errors' % len(errors.errors)

    for error in errors.errors:
        print
        error['file'] = filename
        print 'ERROR in %(file)s: %(start)d - %(end)d (%(error)s)' % error
        for line in error['lines']:
            print '\t%s' % line
        
    return

if __name__ == '__main__':
    main()